/**
 * 
 */
package optimization.naturalOptimization.fitness;

import optimization.IdentificationI;
import optimization.naturalOptimization.population.Population;

/**
 * This class provides the superclass for implementations of fitness functions.
 * 
 * @author Kevin Wagner
 * @version 1.0
 * @param <T>
 *            Type of position to evaluate
 * 
 */
public abstract class SimpleFitnessFunction<T> implements IdentificationI {
	
	private boolean min=true;

	/**
	 * Returns the fitness for the position.
	 * 
	 * @param position
	 *            position to evaluate.
	 * @return fitness value
	 */
	public abstract double getFitness(T position);

	/**
	 * Compares the two given positions.
	 * 
	 * @param fitness1
	 *            first fitness value for comparison
	 * @param fitness2
	 *            second fitness value for comparison
	 * @return 0, if fitness1==fitness2; 1, if fitness1 is better and 2, if
	 *         fitness2 is better
	 */
	public int compare(double fitness1, double fitness2){
		if(fitness1==fitness2){
			return 0;
		}
		if(min){
			if(fitness1<fitness2){
				return 1;
			}else{
				return 2;
			}
		}else{
			if(fitness1<fitness2){
				return 2;
			}else{
				return 1;
			}
		}
	}

	/**
	 * Returns the fitness for the position.
	 * 
	 * @param pop
	 *            Population to calculate fitness.
	 * @throws Exception possible error in multithreading run
	 * 
	 */
	public abstract void getFitness(Population pop) throws Exception;

	/**
	 * @return {@code TRUE} if optimization is a minimization and {@code FALSE} if the optimization is a maximation.
	 */
	public boolean isMin() {
		return min;
	}

	/**
	 * 
	 * @param min status of optimization
	 */
	public void setMin(boolean min) {
		this.min = min;
	}

}
